package com.lipeng.api;

import com.lipeng.bean.Comment;
import com.lipeng.bean.Master;
import com.lipeng.bean.Photo;
import com.lipeng.service.CommentService;
import com.lipeng.service.MasterService;
import com.lipeng.service.PhotoService;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.CopyPic;
import com.lipeng.utils.IPUtils;
import com.lipeng.utils.TimeUtils;
import com.mysql.jdbc.TimeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Created by dllo on 18/3/17.
 */
@Controller
@ResponseBody
@RequestMapping(value = "/api/")
public class PhotoControllerAPI {
    @Resource
    private PhotoService photoService;
    @Resource
    private CommentService commentService;

    //晒图列表
    @RequestMapping(value = "allphoto")
    public AjaxResult allPhoto() {
        AjaxResult result = new AjaxResult();
        List<Photo> photoList = photoService.findAllPhoto();
        if (photoList == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        for (Photo photo : photoList){
            photo.setPic(IPUtils.getIP()+":8080"+photo.getPic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(photoList);
        return result;
    }


    //晒图详情
    @RequestMapping(value = "photodetail")
    public AjaxResult photoDetail(@RequestParam("pid") Integer pid) {
        AjaxResult result = new AjaxResult();
        Photo photo = photoService.selectPhotoById(pid);
        if (photo == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        photo.setPic(IPUtils.getIP()+":8080"+photo.getPic());
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(photo);
        return result;
    }

    //发布晒图
    @RequestMapping(value = "addphoto")
    public AjaxResult addPhoto(@RequestParam("img")MultipartFile file, HttpServletRequest request, @RequestParam("pname") String pname, @RequestParam("uid") Integer uid) throws IOException {
        Photo photo = new Photo();
        photo.setPname(pname);
        photo.setUid(uid);
        photo.setFile(file);

        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "/Users/dllo/Desktop/frame/GameApp/src/main/webapp/images/";
        String targetPath = "/Users/dllo/Desktop/frame/GameApp/target/GameApp/images/";
        //定义 文件名
        String filename = null;
        if (!photo.getFile().isEmpty()) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType = photo.getFile().getContentType();
            //获得文件后缀名
            String suffixName = contentType.substring(contentType.indexOf("/") + 1);
            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            photo.getFile().transferTo(new File(localPath + filename));
            CopyPic.copyFile(localPath+filename,targetPath+filename);
            //把图片的相对路径保存至数据库
            sqlPath = "/images/" + filename;
        }

        photo.setPic(sqlPath);
        photo.setPtime(TimeUtils.time());

        AjaxResult result = new AjaxResult();
        int num = photoService.addPhoto(photo);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("发布失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("发布成功");
        result.setData(photo.getPid());
        return result;
    }


    //删除晒图
    @RequestMapping(value = "delphoto")
    public AjaxResult delPhotoByPid(@RequestParam("pid") Integer pid) {
        AjaxResult result = new AjaxResult();
        Photo photo = photoService.selectPhotoById(pid);
        if (photo.getPic()!=null){
            File file = new File(photo.getPic());
            file.delete();
        }
        int comnum = commentService.delCommentByPid(pid);

        int num = photoService.deletePhotoById(pid);
        if (num < 1) {
            result.setCode("0");
            result.setMessage("删除失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("删除成功");
        return result;
    }


    //晒图评论信息
    @RequestMapping(value = "photocomment")
    public AjaxResult photoComment(@RequestParam("pid") Integer pid) {
        AjaxResult result = new AjaxResult();
        List<Comment> commentList = photoService.selectCommentByPid(pid);
        if (commentList == null) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(commentList);
        return result;
    }

    //添加评论
    @RequestMapping(value = "addcomment")
    public AjaxResult addComment(@RequestParam("uid")Integer uid,
                                 @RequestParam("pid")Integer pid,
                                 @RequestParam("comcontent")String comcontent){
        AjaxResult result = new AjaxResult();
        Comment comment = new Comment();
        comment.setUid(uid);
        comment.setPid(pid);
        comment.setComcontent(comcontent);
        comment.setComtime(TimeUtils.time());

        int num = commentService.addComment(comment);
        if (num<1){
            result.setCode("0");
            result.setMessage("评论失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("评论成功");
        return result;
    }

    //删除评论
    @RequestMapping(value = "delComment")
    public AjaxResult delComment(@RequestParam("comid")Integer comid){
        AjaxResult result = new AjaxResult();
        int num = commentService.deleteCommentById(comid);
        if (num<1){
            result.setCode("0");
            result.setMessage("删除失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("删除成功");
        return result;
    }


}
